package bid

import (
	"strconv"
	"time"
	"wx-api/adx-bid/model/m_adxapp"
	"wx-api/adx-bid/model/m_adxlist"
	"wx-api/adx-bid/model/m_adxslot"
	"wx-api/adx-bid/models/bid/BidModel"
	"wx-api/adx-bid/services/clickhouse_tb"
)

// 媒体请求记录
func MediaReqMsg(syst string, req *BidModel.BidRequest, adx_info *m_adxlist.AdxList, m_info *m_adxapp.AdxApp, s_info *m_adxslot.AdxSlot) {
	tt := time.Now()
	lg := &BidModel.LogMsg{
		Id:        req.GetId(),
		Bidid:     req.GetBidId(),
		Date:      tt.Format("2006-01-02"),
		DateHour:  strconv.Itoa(tt.Hour()),
		Impid:     req.GetImp()[0].GetId(),
		AdxId:     adx_info.Uqid,
		MediaId:   m_info.Uqid,
		SlotId:    s_info.Uqid,
		App:       req.GetApp().GetBundle(),
		Req:       1,
		Timestamp: tt.UnixNano() / 1e6,
		Syst:      syst,
	}
	if syst == "1" {
		clickhouse_tb.SspMsgSend(lg)
	} else if syst == "2" {
		clickhouse_tb.AdxMsgSend(lg)
	} else if syst == "3" {
		clickhouse_tb.DspMsgSend(lg)
	}
}

// 返回媒体广告记录
func MediaResMsg(syst string, req *BidModel.BidRequest, adx_info *m_adxlist.AdxList, m_info *m_adxapp.AdxApp, s_info *m_adxslot.AdxSlot, plid, did, pid string) {
	tt := time.Now()
	lg := &BidModel.LogMsg{
		Id:        req.GetId(),
		Bidid:     req.GetBidId(),
		Date:      tt.Format("2006-01-02"),
		DateHour:  strconv.Itoa(tt.Hour()),
		Impid:     req.GetImp()[0].GetId(),
		AdxId:     adx_info.Uqid,
		MediaId:   m_info.Uqid,
		SlotId:    s_info.Uqid,
		App:       req.GetApp().GetBundle(),
		PlanId:    plid,
		DspId:     did,
		PosId:     pid,
		Fill:      1,
		Timestamp: tt.UnixNano() / 1e6,
		Syst:      syst,
	}
	if syst == "1" {
		clickhouse_tb.SspMsgSend(lg)
	} else if syst == "2" {
		clickhouse_tb.AdxMsgSend(lg)
	} else if syst == "3" {
		lg.AdvId = plid
		lg.PlanId = did
		lg.CreativeId = pid
		lg.Req = 1
		clickhouse_tb.DspMsgSend(lg)
	}
}

// 请求预算记录
func DspReqMsg(syst string, biditem *BudgetService) {
	tt := time.Now()
	lg := &BidModel.LogMsg{
		Id:        biditem.Req.GetId(),
		Bidid:     biditem.Req.GetBidId(),
		Date:      tt.Format("2006-01-02"),
		DateHour:  strconv.Itoa(tt.Hour()),
		Impid:     biditem.Req.GetImp()[0].GetId(),
		AdxId:     biditem.Adx.Uqid,
		MediaId:   biditem.Media.Uqid,
		SlotId:    biditem.Slot.Uqid,
		App:       biditem.Req.GetApp().GetBundle(),
		PlanId:    biditem.Plan.Uqid,
		DspId:     biditem.Dsp.Uqid,
		PosId:     biditem.Pos.Uqid,
		Dreq:      1,
		Timestamp: tt.UnixNano() / 1e6,
		Syst:      "3",
	}
	if syst == "1" {
		clickhouse_tb.SspMsgSend(lg)
	} else if syst == "2" {
		clickhouse_tb.AdxMsgSend(lg)
	}
}

// 预算返回广告记录
func DspResMsg(syst string, biditem *BudgetService) {
	tt := time.Now()
	lg := &BidModel.LogMsg{
		Id:        biditem.Req.GetId(),
		Bidid:     biditem.Req.GetBidId(),
		Date:      tt.Format("2006-01-02"),
		DateHour:  strconv.Itoa(tt.Hour()),
		Impid:     biditem.Req.GetImp()[0].GetId(),
		AdxId:     biditem.Adx.Uqid,
		MediaId:   biditem.Media.Uqid,
		SlotId:    biditem.Slot.Uqid,
		App:       biditem.Req.GetApp().GetBundle(),
		PlanId:    biditem.Plan.Uqid,
		DspId:     biditem.Dsp.Uqid,
		PosId:     biditem.Pos.Uqid,
		Dfill:     1,
		Timestamp: tt.UnixNano() / 1e6,
	}
	if syst == "1" {
		clickhouse_tb.SspMsgSend(lg)
	} else if syst == "2" {
		clickhouse_tb.AdxMsgSend(lg)
	}
}
